<?php

class CAttachment extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	public function _setup(){
		
		$this->_name=R_PRE."attachments";
		
		$this->_primary="attachmentid";
		
		parent::_setup();
		
	}
	
	/* $data=array(
					'0'=>array('itemid'=>'1','attachment_url'=>'dfdsfsdf','ordering'=>'0','intro'=>'dfsdfsdf'),
					'1'=>array('itemid'=>'1','attachment_url'=>'gggggggg','ordering'=>'1','intro'=>'bbbbbbbb')
	*/
	public function createAttachments($data){
		
		foreach($data as $valueArray){
			
			echo $attachmentid=$this->insert($valueArray);
			
			if(!is_numeric($attachmentid)){
				
				return false;
				
			}
			
		}
		
		return true;
		
	}
	
	
	/* $data=array(
					'0'=>array('attachmentid'=>'1','attachment_url'=>'dfdsfsdf','ordering'=>'0','intro'=>'dfsdfsdf'),
					'1'=>array('attachmentid'=>'2','attachment_url'=>'gggggggg','ordering'=>'1','intro'=>'bbbbbbbb')
	表单中attachmentid为空的是新增附件(执行插入操作), 不为空的则为之前的附件(执行修改操作)
	*/
	public function editAttachments($data){
		
		$db=$this->getAdapter();
		
		foreach($data as $valueArray){
			
			if($valueArray['attachmentid']!=""){
			
				$where = $db->quoteInto('attachmentid = ?', $valueArray['attachmentid']);
				
				unset($valueArray['attachment_url']);
				
				unset($valueArray['cut_url']);
				
				unset($valueArray['attachmentid']);
	
				$this->update($valueArray,$where);
			
			}else{
				
				unset($valueArray['attachmentid']);
				
				$this->insert($valueArray);
				
			}
			
		}
		
		return true;		
		
	}
	
	public function getAttachments($itemid,$where=null){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'*');
		
		$select->where("itemid=?",$itemid);
		
		if($where!=null){
			
			$select->where($where);
			
		}
		
		return $db->fetchAll($select);
		
	}
	
	public function getAttachmentByAttachmentid($attachmentid){
		
		$db=$this->getAdapter();
		
		$where=$db->quoteInto(" attachmentid = ? ",$attachmentid);
		
		return $this->fetchRow($where)->toArray();		
		
	}
	
	
	public function delAttachmentsByItemid($itemidArray){
		
		$db=$this->getAdapter();
		
		$itemidString=join(',',$itemidArray);
		
		$where=$db->quoteInto('itemid IN (?)',$itemidArray);
		
		$thisArray=$this->fetchAll($where)->toArray();
		
		Zend_Loader::loadClass("CPath");
		
		for($i=0;$i<count($thisArray);$i++){
			
			unlink(CPath::toAbsoluteRootPath($thisArray[$i]['attachment_url']));
			
			unlink(CPath::toAbsoluteRootPath($thisArray[$i]['cut_url']));
			
		}
		
		$result=$db->query("DELETE FROM $this->_name WHERE itemid IN ($itemidString)");
		
		return $result;
		
	}
	
	public function delAttachmentsById($attachmentIdArray){
		
		$db=$this->getAdapter();
		
		$attachmentIdString=join(',',$attachmentIdArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE attachmentid IN ($attachmentIdString)");
		
		return $result;		
		
	}

	
}